package com.wmh.baseservice.system.controller;

import com.wmh.baseservice.common.annotation.parameterverify.ParameterVerify;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.page.pojo.PageInfo;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ResultCodeInfo;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.system.pojo.SysParams;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import com.wmh.baseservice.system.service.SysParamsService;

/**
 * @author mmx generator
 * @since 2021-02-05
 */
@Api(tags = "系统-参数集")
@RestController
@RequestMapping("system/sysParams")
public class SysParamsController {

    @Resource
    private SysParamsService sysParamsService;


    @PostMapping("admin/list")
    @ApiOperation("系统参数列表")
    public String list(PageInfo pageInfo) {
        PageResult<SysParams> pageResult = new PageUtil<SysParams>(pageInfo).startPage(sysParamsService::page);
        return ReturnBody.success(pageResult);
    }


    @PostMapping("admin/update")
    @ApiOperation("编辑系统参数")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Integer.class, name = "id", value = "系统参数ID", required = true),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "value", value = "系统参数值，当值为多个时，使用json格式或者数组格式", required = true)
    })
    @ParameterVerify(notNull = {"id", "value"})
    public String update(Integer id, String value) {
        SysParams param = sysParamsService.getById(id);
        if (param == null) {
            return ReturnBody.error();
        }
        param.setParamName(value);
        if (!sysParamsService.updateById(param)) {
            return ReturnBody.error();
        }
        return ReturnBody.success();
    }

    @ApiOperation("获取系统参数")
    @PostMapping("getParams")
    @ApiImplicitParams({
             @ApiImplicitParam(paramType = "query", dataTypeClass = Integer.class, name = "type", value = "1:获取banner图", required = true),
     })
    public String getParams(Integer type){
        SysParams sysParams;
        if (type==1){
            sysParams = sysParamsService.getByIdForUpdate(1);
            if (!CommUtil.checkNull(sysParams)){
                return ReturnBody.error(ResultCodeInfo.PARAM_ERROR);
            }
        }else {
            return ReturnBody.error(ResultCodeInfo.PARAM_ERROR);
        }
        return ReturnBody.success(sysParams);
    }
}
